'use strict';

angular.module('app')
	.controller('rechargeRuleController', [ '$rootScope', '$scope', '$http', '$state',
    function($rootScope, $scope, $http, $state) {
        $scope.title = '充值规则';
        $scope.param = {};
        $scope.loading = false;
        $scope.search = function () {
            $.ajax({
                url: $scope.app.project_name + '/rechargeRule/read/list',
                data: $scope.param
            }).then(function (result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                    $scope.pageInfo = result.data;

                } else {
                    $scope.msg = result.msg;
                }

                $(".editSortBtn").show();
                $(".saveSortBtn").hide();
                $scope.$apply();
            });
        }

        $scope.searchRe = function () {
            $scope.loading = true;
            $scope.param.pageNum = 1;
            $.ajax({
                url: $scope.app.project_name + '/rechargeRule/read/list',
                data: $scope.param
            }).then(function (result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                    $scope.pageInfo = result.data;
                } else {
                    $scope.msg = result.msg;
                }

                $(".editSortBtn").show();
                $(".saveSortBtn").hide();
                $scope.$apply();
            });
        }

        $scope.searchRe();

        $scope.operation = function (id, enable) {
            $scope.alertMessage({
                text: "确定做此操作吗？", //内容
                btnCancel: true, //取消按钮
                btnConfirm: true //确认按钮
            }).then(function () {
                $.ajax({
                    url: $scope.app.project_name + '/rechargeRule/update/operation',
                    data: {'ruleId': id, 'enable': enable}
                }).then(function (result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.param.keyword = null;
                        $scope.alertModal({"content": "操作成功"});
                        $scope.search();
                    } else {
                        $scope.alertModal({"content": "操作失败"});
                        $scope.msg = result.msg;
                        $scope.search();
                    }
                    $scope.$apply();
                });
            });
        }

        // 翻页
        $scope.pagination = function (page) {
            $scope.param.pageNum = page;
            $scope.search();
        };

        $scope.clearSearch = function () {
            $scope.param = {};
            $scope.search();
        }

        // 编辑排序
        $scope.editSort=function ($event) {
            if($('#banEnable').val()=='N'){
                $scope.alertModal({'content':'非可用状态不能编辑排序'});
                return;
            }
            if(!$('#banEnable').val()){
                $scope.alertModal({'content':'状态不能为空'});
                return;
            }
            for (var i = 0; i < $scope.pageInfo.records.length; i++) {
                var tr = $(".sort  tbody tr")[i];
                $($(tr).find("td")[7]).hide();
                $($(tr).find("td")[8]).show();
                $(".editSortBtn").hide();
                $(".saveSortBtn").show();
            }
        }
        $scope.saveSort=function ($event) {
            var flag=true;
            var batchUpdates = new Array();//声明一个数组来保存多个的id值
            var sortArray = new Array();//声明一个数组来保存多个序号的值
            for (var j = 0; j < $scope.pageInfo.records.length; j++) {
                var tr = $(".sort  tbody tr")[j];
                var id = $($(tr).find("td")[0]).html();
                var sort = $($(tr).find("input")).val();
                if(sort==null||sort==''){
                     $scope.alertModal({"content": "排序号不能为空"});
                    return;
                }

                sortArray[j]=sort;//用来存放序号
                var batchUpdate = id + "-" + sort;
                batchUpdates[j] = batchUpdate;
                if (!isNaN(sort) && sort > 0 && sort <= $scope.pageInfo.records.length) {
                    continue;
                } else {
                   /* $scope.alertModal({"content": "数据格式输入有误或序号大于了现有的最大的条数"});
                    flag = false;*/
                }
            }
            var s = sortArray;//用来存放序号的值
            var count=0;
            for(var m=0;m<s.length;m++) {//验证是否有重复的。如果有的话就加一
                if (s[m] == s[m + 1]) {
                    count++;
                }
            }
            if(count!=0){
                $scope.alertModal({"content": "序号重复，请重新排序"});
                flag = false;
            }
            if (flag) {
                $.ajax({
                    url: $scope.app.project_name + '/rechargeRule/update/editSort',
                    data: {'ids': batchUpdates},
                    traditional: true
                }).then(function (result) {
                    console.log(result);
                    if (result.httpCode == 200) {
                        $scope.alertModal({"content": "操作成功"});
                        $(".editSortBtn").show();
                        $(".saveSortBtn").hide();

                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.search();
                    $scope.$apply();
                });
            }
        }



                                    }]);